<template>
  <div class="home">
    <ul class="tabs">
      <li
        @click="switchTab(index)"
        :class="
          tabActive === index ? ' tab_item active ' : ' tab_item default '
        "
        v-for="(tab, index) in tabList"
        :key="index"
      >
        {{ tab.name }}
      </li>
    </ul>
    <div class="container">
      <router-view />
    </div>
  </div>
</template>

<script>
import supplyApi from "../apis/supplyApi";

export default {
  name: "Home",
  data() {
    return {
      tabActive: 0,
      tabList: [
        {
          name: "销售",
          path: "/sold",
        },
        {
          name: "供给",
          path: "/supply",
        },
        {
          name: "买受人",
          path: "/buyer",
        },
        {
          name: "项目",
          path: "/project",
        },
        {
          name: "从业主体",
          path: "/practitionSubject",
        },
      ],
    };
  },
  created() {
    let tabInfo = JSON.parse(localStorage.getItem("tabInfo"));
    if (tabInfo !== null) {
      this.tabActive = +tabInfo.tabIndex;
      this.$router.push(tabInfo.path);
    } else {
      this.tabActive = 0;
      this.$router.push(this.tabList[0].path);
    }
    window.addEventListener("beforeunload", () => {
      let localStorage = localStorage || window.localStorage;
      localStorage.removeItem("tabInfo");
    });
  },
  mounted() {
    supplyApi.test().then((res) => {
      console.log("测试请求结果：", res);
    });
  },
  destroyed() {
    window.removeEventListener("beforeunload");
  },
  methods: {
    switchTab(index) {
      let localStorage = localStorage || window.localStorage;
      this.tabActive = index;
      localStorage.setItem("tabInfo", JSON.stringify(this.tabList[index]));
      this.$router.push(this.tabList[index].path);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../common/styles.scss";
.tabs {
  background: #1677fc;
  height: 70px;
  padding-bottom: 15px;
  @include fl();
  .tab_item {
    flex: 1;
    text-align: center;
  }
  .default {
    font-size: 14px;
    color: #ffffff;
    opacity: 0.6;
  }
  .active {
    position: relative;
    font-size: 16px;
    color: #ffffff;
    &::after {
      content: "";
      display: block;
      position: absolute;
      background: #ffffff;
      width: 15px;
      height: 2px;
      left: 50%;
      transform: translateX(-50%);
      bottom: -6px;
    }
  }
}
.container {
  background: #edeff5;
  overflow: hidden;
  margin-top: -15px;
  box-sizing: border-box;
  padding: 0 10px;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
}
</style>
